package sdcncsi.ict.counter.web;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.Map;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.view.AbstractView;


public class ImageView extends AbstractView {

	//渲染输出方法
	protected void renderMergedOutputModel(Map dataMap, HttpServletRequest request, 
			HttpServletResponse response) throws Exception {
		//设定response属性
		response.setContentType("image/jpeg");
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		//得到response输出流
		ServletOutputStream responseOutputStream = response.getOutputStream();
		
		// 在内存中创建图象
		int width = 80, height = 20;
		BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		// 获取图形上下文
		Graphics g = image.getGraphics();
		g.setColor(ColorTool.getRandColor(200, 250));// 设定背景色
		g.fillRect(0, 0, width, height);
		g.setFont(new Font("Times New Roman", Font.PLAIN, 18));//设定字体

		//绘制图像:计数值，或者错误提示
		if((Boolean)dataMap.get("result")){
			String pvNum = dataMap.get("count").toString();//得到PageView计数 
			g.setColor(ColorTool.getRandColor(10, 60));//随机生成文字颜色
			g.drawString(pvNum, 5, 16);
		}else{
			g.setColor(Color.red);
			g.drawString("Error", 5, 16);
		}
		
		// 图象生效
		g.dispose();
		// 输出图象到页面
		ImageIO.write(image, "JPEG", responseOutputStream);
		//以下关闭输入流！
		responseOutputStream.flush();
		responseOutputStream.close();
	}

}
